/**
 * 
 */
package servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import action.Action;



import utils.DbAccess;



public class DoServlet extends HttpServlet implements Servlet {
	private static final long serialVersionUID = 725332764942513392L;
	
	public static final String DBNAME = "user";
	DbAccess db = null;
	
	public void init() {
		try {
			db = new DbAccess(DBNAME,true);
		} catch ( Exception e) {
			e.printStackTrace();
		}
	}

	protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		//res.getWriter().write("<h1>doGet</h1>");
		process(req,res);
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		//res.getWriter().write("<h1>doPost</h1>");
		process(req,res);
	}

	private void process(HttpServletRequest req, HttpServletResponse res) throws UnsupportedEncodingException {
		res.setHeader("Cache-Control", "no-cache");
		req.setCharacterEncoding("utf-8");
		res.setCharacterEncoding("utf-8");
		res.setContentType("text/xml");
		String xml="";
		
		Connection conn=null;
		try {
			conn = db.getConn();
			String act = req.getParameter("act");
			if (act.equals("insert")){
				xml = new Action().insert(req, res, conn);
			} else if(act.equals("list")){
				xml = new Action().list(req, res, conn);
			} else if(act.equals("delete")){
				xml = new Action().delete(req, res, conn);
			}
			res.getWriter().write(xml);
			res.getWriter().close();
		//	Common.print(req, res, xml, "utf-8", "text/xml", false);
		} catch (Exception e) {

		} finally {
			if(conn!=null) try{conn.close();}catch (Exception e) {}
		}
		
	}

}
